/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@libresoft.es>
 *
 */

package com.LibreGeoSocial.App;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.LibreGeoSocial.Api.Layers.LibreGeoSocialLayer;
import com.LibreGeoSocial.Api.Layers.PanoramioLayer;
import com.LibreGeoSocial.Api.Layers._11870Layer;



public class LayerList extends ListActivity {

	private static final int MENU_ADD_LAYER = Menu.FIRST + 1;
	private static final int DIALOG_ADD_LAYER = 1;
	
	private LayerAdapter mAdapter;
	
	public void onCreate(Bundle savedInstanceState) {

		// Init
		super.onCreate(savedInstanceState);
		
 		// Adapter for ListView
        mAdapter = new LayerAdapter(this);
        setListAdapter(mAdapter);
		
	}
	
    protected void onListItemClick(ListView l, View v, int position, long id) {
        
    	if (position==0)
    	{
    		Intent IntentLGS = new Intent(this, new NodeList().getClass() );
    		LibreGeoSocialLayer layer1 = new LibreGeoSocialLayer("lgs",5.0,"");
    		IntentLGS.putExtra ("layer",layer1);
    		
    		startActivity(IntentLGS);
    	}
    	else if (position==1)
    	{
    		
    		Intent Intent11870 = new Intent(this, new NodeList().getClass() );
    		_11870Layer layer = new _11870Layer("11870",10.0,"");
    		Intent11870.putExtra ("layer",layer);

    		startActivity(Intent11870);
    	}
    	
    	else if (position==2)
    	{
       		Intent IntentPanoramio = new Intent(this, new NodeList().getClass() );
       		PanoramioLayer layer = new PanoramioLayer("panoramio",10.0,"");
       		IntentPanoramio.putExtra ("layer",layer);

    		startActivity(IntentPanoramio);
    		
    	}
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	menu.addSubMenu (0, MENU_ADD_LAYER, 0 , "Add Layer")
		  .setIcon(R.drawable.layer_plus_48);
    	
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	
    	switch (item.getItemId()) {
    	
    		case MENU_ADD_LAYER:
    			showDialog(DIALOG_ADD_LAYER);
    		
    	}
    	
    	return true;
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        
    	switch (id) {
    	
	    	case DIALOG_ADD_LAYER:
	    		
	    		LayoutInflater factory1 = LayoutInflater.from(this);
	    		final View textEntryView1 = factory1.inflate(R.layout.custom_dialog, null);
	    		
	    		TextView text = (TextView) textEntryView1.findViewById (R.id.dialog_text);
	    		ImageView image = (ImageView) textEntryView1.findViewById(R.id.dialog_image);
	
	    		text.setText("Soon, you can add and upload your layer with your own content!!" +	    				     
	    				     "\n\n"+
	    				     "The developers of LibreGeoSocial.");
	
	    		
		        return new AlertDialog.Builder(this)	        
		        .setTitle("Your own layers!")		        
		        .setView(textEntryView1) 
		        .setIcon(R.drawable.logo)
		        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int whichButton) {
		            	
		            }
		        })
			    .create();
    	}
    	
    	return null;
    }
	public static class LayerAdapter extends BaseAdapter {

		private Context mContext;
		
		public LayerAdapter(Context c) {
            mContext = c;
        }

		public int getCount() {
			// TODO Auto-generated method stub
			return 3;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view;
			 
	        if (convertView == null) {
	            // Make up a new view
	            LayoutInflater inflater = (LayoutInflater) mContext
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            view = inflater.inflate(R.layout.layer_list, null);
	        } else {
	            // Use convertView if it is available
	            view = convertView;
	        }
			
	        ImageView img = (ImageView) view.findViewById(R.id.image);
	        TextView title = (TextView) view.findViewById(R.id.title);
	        TextView description = (TextView) view.findViewById(R.id.description);
	        ImageView arrow = (ImageView) view.findViewById(R.id.arrow);
	  
	        if (position==0)
	        {
	        	//LibreGeoSocialLayer
	        	img.setImageResource(R.drawable.icon_70);
	        	title.setText("LibreGeoSocial");
	        	description.setText("All nearby nodes of LGS");
	        }
	        else if(position==1)
	        {
	        	//11870Layer
	        	img.setImageResource(R.drawable.img_11870);
	        	title.setText("11870.com");
	        	description.setText("The services in your area from 11870");
	        }
	        else if(position==2)
	        {
	        	//11870Layer
	        	img.setImageResource(R.drawable.panoramio);
	        	title.setText("Panoramio");
	        	description.setText("Your favorites places from Panoramio");		
	        }
	        
	        arrow.setImageResource(R.drawable.arrow);
	        
	        return view;

		}

	}
}
